from character import *
import hashlib

def checkLoad():
    try:
        hash = open('save.hs', 'r')
        sha = hashlib.sha256()
        file = open('save.dat', 'r')
        sha.update(file.read())

        if (hash.read() == sha.hexdigest()):
            return True
        else:
            return False
        
    except IOError:
        return False

def loadGame():

    player = Character(0, 0, 0, 0, 0, "null")
    
    file = open('save.dat', 'r')

    strengthOnFile = 0

    dexterityOnFile = 10

    vitalityOnFile = 20

    energyOnFile = 30

    maxHealthOnFile = 40

    healthOnFile = 50

    maxManaOnFile = 60

    manaOnFile = 70

    defenseOnFile = 80

    magdefOnFile = 90

    atkMeleeMinOnFile = 100

    atkMeleeMaxOnFile = 110

    atkRangedMinOnFile = 120

    atkRangedMaxOnFile = 130

    atkMagicMinOnFile = 140

    atkMagicMaxOnFile = 150

    goldOnFile = 160

    evasionOnFile = 170

    accuracyOnFile = 180

    criticalRateOnFile = 190

    nameOnFile = 20000

    file.seek(strengthOnFile)
    player.strength = float(file.read(10))

    file.seek(dexterityOnFile)
    player.dexterity = float(file.read(10))

    file.seek(vitalityOnFile)
    player.vitality = float(file.read(10))

    file.seek(energyOnFile)
    player.energy = float(file.read(10))

    file.seek(maxHealthOnFile)
    player.maxHealth = float(file.read(10))

    file.seek(healthOnFile)
    player.health = float(file.read(10))

    file.seek(maxManaOnFile)
    player.maxMana = float(file.read(10))

    file.seek(manaOnFile)
    player.mana = float(file.read(10))

    file.seek(defenseOnFile)
    player.defense = float(file.read(10))

    file.seek(magdefOnFile)
    player.magdef = float(file.read(10))

    file.seek(atkMeleeMinOnFile)
    player.atkMeleeMin = float(file.read(10))

    file.seek(atkMeleeMaxOnFile)
    player.atkMeleeMax = float(file.read(10))

    file.seek(atkRangedMinOnFile)
    player.atkRangedMin = float(file.read(10))

    file.seek(atkRangedMaxOnFile)
    player.atkRangedMax = float(file.read(10))

    file.seek(atkMagicMinOnFile)
    player.atkMagicMin = float(file.read(10))

    file.seek(atkMagicMaxOnFile)
    player.atkMagicMax = float(file.read(10))

    file.seek(goldOnFile)
    player.gold = float(file.read(10))

    file.seek(evasionOnFile)
    player.evasion = float(file.read(10))

    file.seek(accuracyOnFile)
    player.accuracy = float(file.read(10))

    file.seek(criticalRateOnFile)
    player.criticalRate = float(file.read(10))

    file.seek(nameOnFile)
    player.name = file.read()

    return player
